package role

import (
	"server/game/cache"
	"server/game/model"
	"server/share/config"

	"github.com/hwcer/cosgo/registry"
	"github.com/hwcer/cosgo/slice"
	"github.com/hwcer/yyds/context"
	"github.com/hwcer/yyds/errors"
	"github.com/hwcer/yyds/players/player"
)

func init() {
	Register(&setList{})
}

type setList struct {
}

func (r *setList) Caller(node *registry.Node, ctx *context.Context) interface{} {
	f := node.Method().(func(*setList, *context.Context) interface{})
	return f(r, ctx)
}

/**
 * @name setAchievementBall
 * @param string achievementBall 成就球数据,用','分割。
 * 设置成就球
 */
func (r *setList) SetAchievementBall(c *context.Context) interface{} {
	s := c.GetString("achievementBall")
	if s == "" {
		return nil
	}
	arr := slice.SplitAndUnrepeated(s)
	if len(arr) > 10 {
		return nil
	}
	if err := r.verify(c.Player, config.ITypeAchievementBall, arr...); err != nil {
		return err
	}
	role := cache.GetRole(c.Player.Updater)
	role.Set("achievementBall", arr)
	return true
}

/**
 * @name SetReputation
 * @param string reputation 星光体绰号,用','分割。
 * 设置星光体绰号
 */

func (r *setList) SetReputation(c *context.Context) interface{} {
	s := c.GetString("reputation")
	if s == "" {
		return nil
	}
	arr := slice.SplitAndUnrepeated(s)
	if len(arr) > 7 {
		return nil
	}
	if err := r.verify(c.Player, config.ITypeNickName, arr...); err != nil {
		return err
	}

	role := cache.GetRole(c.Player.Updater)
	role.Set("reputation", arr)
	return true
}

/**
 * @name setAstralImage
 * @param string image 星光体形象,用','分割
 * 设置星光体装扮
 */
func (r *setList) SetAstralImage(c *context.Context) interface{} {
	s := c.GetString("image")
	if s == "" {
		return nil
	}
	arr := slice.SplitAndUnrepeated(s)
	if len(arr) > 7 {
		return errors.ErrArgEmpty
	}
	if err := r.verify(c.Player, config.ITypePlayerCloth, arr...); err != nil {
		return err
	}

	role := cache.GetRole(c.Player.Updater)
	role.Set("astralImage", arr)
	return true
}

/**
 * @name setIcon
 * @param string iconID  头像ID
 * 设置玩家头像
 */

func (r *setList) SetIcon(c *context.Context) interface{} {
	id := c.GetInt32("iconID")
	if id == 0 {
		return nil
	}
	if err := r.verify(c.Player, config.ITypeHeadIcon, id); err != nil {
		return err
	}

	role := cache.GetRole(c.Player.Updater)
	role.Set("icon", id)
	return true
}

/**
 * @name setIconFrame
 * @param string iconFrameID  头像框ID
 * 设置玩家头像框
 */
func (r *setList) SetIconFrame(c *context.Context) interface{} {
	id := c.GetInt32("iconFrameID")
	if id == 0 {
		return nil
	}
	if err := r.verify(c.Player, config.ITypeHeadFrame, id); err != nil {
		return err
	}

	role := cache.GetRole(c.Player.Updater)
	role.Set("iconFrame", id)
	return true
}

/**
 * @name businessCard
 * @param string businessCardID  名片ID
 * 设置玩家名片
 */
func (r *setList) BusinessCard(c *context.Context) interface{} {
	id := c.GetInt32("businessCardID")
	if id == 0 {
		return nil
	}
	if err := r.verify(c.Player, config.ITypeBusinessCard, id); err != nil {
		return err
	}

	role := cache.GetRole(c.Player.Updater)
	role.Set("businessCardID", id)
	return true
}

//
///**
// * @name setStarSkill
// * @param int skillID  星光体技能ID
// * 设置玩家星光体技能
// */
//func (r *setList) SetStarSkill(c *context.Context) interface{} {
//	id := c.GetInt32("skillID")
//	if id == 0 {
//		return nil
//	}
//	doc := cache.GetRole(c.Player.Updater)
//	role := doc.All()
//	starSkill := role.StarSkill
//	if starSkill.LastUseTime+1000 > c.Unix() {
//		return c.Error("技能冷却中")
//	}
//	//TODO:需要去天赋树里验证是否有此技能
//	starSkill.Id = id
//	starSkill.LastUseTime = 0
//
//	doc.Set("starSkill", starSkill)
//	return true
//}
//
///**
// * @name useStarSkill
// * @param int skillID  星光体技能ID
// * 使用玩家星光体技能
// */
//
//func (r *setList) UseStarSkill(c *context.Context) interface{} {
//	id := c.GetInt32("skillID")
//	if id == 0 {
//		return nil
//	}
//	doc := cache.GetRole(c.Player.Updater)
//	role := doc.All()
//	starSkill := role.StarSkill
//	if starSkill.Id != id {
//		return c.Error("无法使用")
//	}
//	if starSkill.LastUseTime+1000 > c.Unix() {
//		return c.Error("技能冷却中")
//	}
//	//TODO:需要去天赋树里验证是否有此技能
//	starSkill.Id = id
//	starSkill.LastUseTime = c.Unix()
//
//	doc.Set("starSkill", starSkill)
//	return true
//}

/**
 * @name setAppUnit
 * @param string key  设置key
 * @param int data  设置内容
 * 设置玩家app助理
 */
func (r *setList) SetAppUnit(c *context.Context) interface{} {
	key := c.GetString("key")
	data := c.GetInt32("data")

	var it int32
	if key == "backgroud" {
		it = config.ITypeAppBackground
	} else {
		it = config.ITypeCollectUnit
	}
	if err := r.verify(c.Player, it, data); err != nil {
		return err
	}

	doc := cache.GetRole(c.Player.Updater)
	role := doc.All()
	appUnit := role.AppUnit

	switch key {
	case "backgroud":
		appUnit.BackGroud = data
	case "unit01":
		appUnit.Unit01 = data
	case "unit02":
		appUnit.Unit02 = data
	default:
		return c.Error("配置不存在")

	}

	doc.Set("appUnit", appUnit)
	return true
}

/**
 * @name setpackage
 * @param int data 成就包装袋ID
 * 设置成就包装袋
 */

func (r *setList) SetPackage(c *context.Context) interface{} {
	id := c.GetInt32("data")
	if id == 0 {
		return errors.ErrArgEmpty
	}
	if err := r.verify(c.Player, config.ITypeAchievementPacket, id); err != nil {
		return err
	}

	role := cache.GetRole(c.Player.Updater)
	role.Set("package", id)
	return true
}

/**
 * @name setSpaceUnit
 * @param int data 个人空间助理ID
 * 设置个人空间助理
 */

func (r *setList) SetSpaceUnit(c *context.Context) interface{} {
	id := c.GetInt32("data")
	if id <= 0 {
		return errors.ErrArgEmpty
	}

	if err := r.verify(c.Player, config.ITypeCollectUnit, id); err != nil {
		return err
	}

	role := cache.GetRole(c.Player.Updater)
	role.Set("spaceUnit", id)
	return true
}

/**
 * @name setSpaceProgress
 * @param array data 完全数据
 * 空间进度设置数据
 */

func (r *setList) SetSpaceProgress(c *context.Context) interface{} {
	var ss []int32
	if err := c.Bind(&ss); err != nil {
		return err
	}
	if len(ss) > 2 {
		return errors.ErrArgEmpty
	}
	role := cache.GetRole(c.Player.Updater)
	role.Set("spaceProgress", ss)
	return true
}

func (r *setList) verify(p *player.Player, it int32, keys ...int32) error {
	if len(keys) == 0 {
		return nil
	}
	mod := model.Collect{}
	coll := p.Collection(config.ITypeCollect)
	var ks []int32
	for _, k := range keys {
		if k > 0 {
			if it != 0 && mod.IType(k) != it {
				return errors.ErrConfigEmpty
			}
			ks = append(ks, k)
			coll.Select(k)
		}
	}
	if len(ks) == 0 {
		return nil
	}
	if err := coll.Data(); err != nil {
		return err
	}
	for _, k := range ks {
		if coll.Val(k) == 0 {
			return errors.ErrDataNotExists
		}
	}
	return nil
}
